import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:intl/intl.dart';
import 'package:seedfour/app/theme.dart';
import 'package:seedfour/app/views/module_notice/notice_detail/state.dart';
import 'package:url_launcher/url_launcher.dart';

import '../../../../../toolkit/typedef/function.dart';
import '../../../../constants.dart';

class ReceiveInfoWidget extends StatelessWidget {

  final ParamSingleCallback onClickCase;

  const ReceiveInfoWidget({
    Key? key,
    required this.data,
    required this.onClickCase,
  }) : super(key: key);

  final NoticeDetailState data;

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: EdgeInsets.symmetric(horizontal: kSpaceSmall),
      padding: EdgeInsets.all(kSpaceMedium),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(kRadiusMedium),
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Text(
                "当前状态",
                style: TextStyle(
                  fontSize: kFontSizeSmall,
                  color: AppColor.colorFontBlack,
                ),
              ),
              Text(data.noticeStatus[data.noticeData.value.status]!.$1 ?? '',
                  style: TextStyle(
                    fontSize: kFontSizeSmall,
                    color: data.noticeStatus[data.noticeData.value.status]!.$2,
                  )),
            ],
          ),
          SizedBox(height: kSpaceLarge),
          switch (data.noticeData.value.status) {
            0 => _buildUnreceivedView(),
            1 => _buildGotView(),
            2 => _buildGotView(),
            int() => throw UnimplementedError(),
            null => throw UnimplementedError(),
          },
        ],
      ),
    );
  }

  Widget _buildUnreceivedView() {
    return Column(
      children: [
        Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Text(
              '所需点数',
              style: TextStyle(
                fontSize: kFontSizeSmall,
                color: AppColor.colorFontBlack,
              ),
            ),
            Text(
              data.noticeData.value.needQuota.toString(),
              style: TextStyle(
                fontSize: kFontSizeSmall,
                color: AppColor.colorFontBlack,
              ),
            ),
          ],
        ),
        SizedBox(height: kSpaceLarge),
        Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Text(
              '你的可用点数',
              style: TextStyle(
                fontSize: kFontSizeSmall,
                color: AppColor.colorFontBlack,
              ),
            ),
            Text(
              data.noticeData.value.quota.toString(),
              style: TextStyle(
                fontSize: kFontSizeSmall,
                color: AppColor.colorFontBlack,
              ),
            ),
          ],
        ),
      ],
    );
  }

  Widget _buildGotView() {
    return Column(
      children: [
        Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Text(
              '领取人',
              style: TextStyle(
                fontSize: kFontSizeSmall,
                color: AppColor.colorFontBlack,
              ),
            ),
            Text(
              data.noticeData.value.receiveName ?? '',
              style: TextStyle(
                fontSize: kFontSizeSmall,
                color: AppColor.colorFontBlack,
              ),
            ),
          ],
        ),
        SizedBox(height: kSpaceLarge),
        Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Text(
              '领取人电话',
              style: TextStyle(
                fontSize: kFontSizeSmall,
                color: AppColor.colorFontBlack,
              ),
            ),
            InkWell(
              onTap: () async {
                final Uri phoneUri = Uri(
                    scheme: 'tel', path: data.noticeData.value.receivePhone);
                if (await canLaunchUrl(phoneUri)) {
                  await launchUrl(phoneUri);
                } else {
                  EasyLoading.showToast('无法拨打电话');
                }
              },
              child: Text(
                data.noticeData.value.receivePhone ?? '',
                style: TextStyle(
                  fontSize: kFontSizeSmall,
                  color: AppColor.colorFontBlue,
                  decoration: TextDecoration.underline,
                ),
              ),
            ),
          ],
        ),
        SizedBox(height: kSpaceLarge),
        Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Text(
              '领取时间',
              style: TextStyle(
                fontSize: kFontSizeSmall,
                color: AppColor.colorFontBlack,
              ),
            ),
            Text(
              DateFormat('yyyy.MM.dd HH:mm').format(
                DateTime.fromMillisecondsSinceEpoch(
                    data.noticeData.value.receiveTime!),
              ),
              style: TextStyle(
                fontSize: kFontSizeSmall,
                color: AppColor.colorFontBlack,
              ),
            ),
          ],
        ),
        if (data.noticeData.value.status ==
            data.noticeStatus.keys.elementAt(2)) ...[
          SizedBox(height: kSpaceLarge),
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Text(
                '案件',
                style: TextStyle(
                  fontSize: kFontSizeSmall,
                  color: AppColor.colorFontBlack,
                ),
              ),
              SizedBox(width: kSpaceBig),
              Expanded(
                child: InkWell(
                  onTap: () => onClickCase(data.noticeData.value.caseId),
                  child: Text(
                    textAlign: TextAlign.right,
                    data.noticeData.value.caseName ?? '',
                    style: TextStyle(
                      fontSize: kFontSizeSmall,
                      color: AppColor.colorFontBlue,
                      decoration: TextDecoration.underline,
                    ),
                  ),
                ),
              ),
            ],
          ),
          SizedBox(height: kSpaceLarge),
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Text(
                '成交时间',
                style: TextStyle(
                  fontSize: kFontSizeSmall,
                  color: AppColor.colorFontBlack,
                ),
              ),
              Text(
                DateFormat('yyyy.MM.dd HH:mm').format(
                  DateTime.fromMillisecondsSinceEpoch(
                      data.noticeData.value.refreshAt!),
                ),
                style: TextStyle(
                  fontSize: kFontSizeSmall,
                  color: AppColor.colorFontBlack,
                ),
              ),
            ],
          ),
        ],
      ],
    );
  }
}
